package com.hundsun.fcp.common;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import com.hundsun.fcp.activity.LockPatternCreateActivity;
import com.hundsun.fcp.base.BaseActivity;
import com.hundsun.fcp.common.AppointFiled.RequestCodeDef;
import com.hundsun.fcp.common.utils.CommonDialog;
import com.hundsun.fcp.common.utils.ToastUtil;
import com.hundsun.fcp.dao.MineDao;
import com.hundsun.fcp.dialog.LoadingDialog;
import com.hundsun.fcp.manager.FcpUserManager;
import com.hundsun.fcp.manager.SettingManager;
import com.hundsun.fcp.openapi.IOpenApiDataServiceCallback;
import com.hundsun.fcp.openapi.trade.TradeChangeUserItemDataService;
import com.hundsun.fcp.openapi.trade.TradeLoginDataService;
import com.hundsun.fcp.openapi.trade.TradeSmsDataService;
import com.hundsun.fcp.openapi.trade.bean.RequestLoginBean;
import com.hundsun.fcp.openapi.trade.bean.RequestSmsBean;
import com.hundsun.fcp.openapi.trade.bean.RequestUserItemBean;
import com.hundsun.fcp.openapi.trade.bean.ResponseLoginBean;
import com.hundsun.fcp.openapi.trade.bean.Result;
import com.hundsun.fcp.widget.TimerButton;

public class TradeRequest {
	
	/**
	 * 短信验证码
	 * @throws Exception 
	 */
	public static void obtainSms(final Context pContext,String mobile,String transcode,final TimerButton timerButton) {
		TradeSmsDataService smsDataService = new TradeSmsDataService(pContext) ;
		smsDataService.setCallback(new IOpenApiDataServiceCallback<Result>() {
			
			@Override
			public void onGetError(int errorCode, String errorMsg, Throwable error) {
				ToastUtil.showInfoToast((Activity)pContext, errorMsg, 0) ;
				timerButton.onDestory() ;
			}
			
			@Override
			public void onGetData(Result data) {
				if(Result.SUCCESS.equals(data.state)){
						
				}else{
					ToastUtil.showInfoToast((Activity)pContext, ((Result)data).info, 0) ;
					timerButton.onDestory() ;
				}
			}
		}) ;
		RequestSmsBean entity = new RequestSmsBean() ;
		entity.mobile = mobile ;
		entity.transcode = transcode ;
		smsDataService.cancel() ;
		smsDataService.post(entity) ;
		
	}
	
	/**
	 * 修改用户信息
	 * @param activity
	 * @param bean
	 * @param showLoading
	 */
	public static void doUpdateUserItem(final BaseActivity activity,final RequestUserItemBean bean,final boolean showLoading){
		final TradeChangeUserItemDataService service = new TradeChangeUserItemDataService(activity) ;
		if(showLoading){
			LoadingDialog.showLoading(activity, "正在修改...", new DialogInterface.OnCancelListener() {
				
				@Override
				public void onCancel(DialogInterface dialog) {
					service.cancel() ;
				}
			}) ;
		}
		service.setCallback(new IOpenApiDataServiceCallback<Result>() {
			
			@Override
			public void onGetError(int errorCode, String errorMsg, Throwable error) {
				LoadingDialog.dismiss() ;
				ToastUtil.showInfoToast(activity,errorMsg, 1) ;
			}
			
			@Override
			public void onGetData(Result data) {
				LoadingDialog.dismiss() ;
				if(data.isSuccess()){
					activity.setResult(activity.RESULT_OK);
					activity.finish() ;
				}else{
					ToastUtil.showInfoToast(activity,data.info, 1) ;
				}
			}
		}) ;
		
		service.cancel() ;
		service.post(bean) ;
	}
	
	/**
	 * 登录
	 * @param activity
	 * @param loginBean
	 * @param isLockTip 是否提示用户设置手势密码
	 */
	public static void doLogin(final BaseActivity activity,final RequestLoginBean loginBean,final boolean isLockTip,final boolean showLoading){
		final TradeLoginDataService mLoginDataService = new TradeLoginDataService(activity) ;
		if(showLoading){
			LoadingDialog.showLoading(activity, "正在登录...", new DialogInterface.OnCancelListener() {
				
				@Override
				public void onCancel(DialogInterface dialog) {
					mLoginDataService.cancel() ;
				}
			}) ;
		}
		
		mLoginDataService.setCallback(new IOpenApiDataServiceCallback<ResponseLoginBean>() {
			
			@Override
			public void onGetError(int errorCode, String errorMsg, Throwable error) {
				LoadingDialog.dismiss() ;
				ToastUtil.showInfoToast(activity, errorMsg, 1) ;
			}
			
			@Override
			public void onGetData(ResponseLoginBean data) {
				LoadingDialog.dismiss() ;
				if(data.isSuccess()){
					FcpUserManager.getInstance().storeUserInfo(data.clientno, loginBean.mobile, loginBean.password) ;
					//数据库存储
					MineDao mineDao = new MineDao(activity);
					mineDao.insertMineResult(data) ; 
					if(isLockTip){
						if (SettingManager.AppSetting.promptCreateLockPattern() && !SettingManager.FcpUserSetting.hasLockPattern() ){
							CommonDialog.showSettingPattern(activity,new DialogInterface.OnClickListener(){
								@Override
								public void onClick(DialogInterface dialog,int which) {
									SettingManager.AppSetting.setPromptCreateLockPattern(false);
									activity.afterLogin();
								}
							}, new DialogInterface.OnClickListener(){
								@Override
								public void onClick(DialogInterface dialog,int which) {
									Intent intent = new Intent(activity, LockPatternCreateActivity.class);
									activity.startActivityForResult(intent, RequestCodeDef.PARTTERN_REQUEST_CODE_CREATE);
								}
							}, new DialogInterface.OnClickListener(){
								@Override
								public void onClick(DialogInterface dialog,int which) {
									activity.afterLogin();
								}
							}) ;
						}else{
							activity.afterLogin();
						}
					}else{
						activity.afterLogin();
					}
				}else{
					FcpUserManager.getInstance().cleanStoredUserInfo() ;
					ToastUtil.showInfoToast(activity, "登录失败."+data.info, 1) ;
					Redirect.startLogin(activity) ;
				}
			}
		}) ;
		mLoginDataService.post(loginBean) ;
	}
}
